﻿package morn.core.components
{
    import __AS3__.vec.*;
    import flash.display.*;
    import flash.events.*;
    import morn.core.events.*;
    import morn.core.handlers.*;
    import morn.core.utils.*;

    public class Clip extends Component implements IClip, ISkinAbleContainer
    {
        protected var _autoStopAtRemoved:Boolean = true;
        protected var _bitmap:AutoBitmap;
        protected var _clipX:int = 1;
        protected var _clipY:int = 1;
        protected var _clipWidth:Number;
        protected var _clipHeight:Number;
        protected var _url:String;
        protected var _autoPlay:Boolean;
        protected var _interval:int;
        protected var _from:int = -1;
        protected var _to:int = -1;
        protected var _complete:Handler;
        protected var _isPlaying:Boolean;
        private var _skinChanged:Boolean = false;

        public function Clip(param1:String = null, param2:int = 1, param3:int = 1)
        {
            this._interval = MornConfig.MOVIE_INTERVAL;
            this._clipX = param2;
            this._clipY = param3;
            this.url = param1;
            return;
        }// end function

        public function setSkin(param1:Object) : void
        {
            if (!this._skinChanged)
            {
                this.url = param1 as String;
            }
            return;
        }// end function

        public function set skinChanged(param1:Boolean) : void
        {
            this._skinChanged = param1;
            return;
        }// end function

        override protected function createChildren() : void
        {
            var _loc_1:* = new AutoBitmap();
            this._bitmap = new AutoBitmap();
            addChild(_loc_1);
            return;
        }// end function

        override protected function initialize() : void
        {
            addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
            addEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
            return;
        }// end function

        protected function onAddedToStage(event:Event) : void
        {
            if (this._autoPlay)
            {
                this.play();
            }
            return;
        }// end function

        protected function onRemovedFromStage(event:Event) : void
        {
            if (this._autoStopAtRemoved)
            {
                this.stop();
            }
            return;
        }// end function

        public function get url() : String
        {
            return this._url;
        }// end function

        public function set url(param1:String) : void
        {
            if (this._url != param1 && Boolean(param1))
            {
                this._url = param1;
                callLater(this.changeClip);
            }
            this._skinChanged = true;
            this._bitmap.url = this._url;
            return;
        }// end function

        public function get clipX() : int
        {
            return this._clipX;
        }// end function

        public function set clipX(param1:int) : void
        {
            if (this._clipX != param1)
            {
                this._clipX = param1;
                callLater(this.changeClip);
            }
            return;
        }// end function

        public function get clipY() : int
        {
            return this._clipY;
        }// end function

        public function set clipY(param1:int) : void
        {
            if (this._clipY != param1)
            {
                this._clipY = param1;
                callLater(this.changeClip);
            }
            return;
        }// end function

        public function get clipWidth() : Number
        {
            return this._clipWidth;
        }// end function

        public function set clipWidth(param1:Number) : void
        {
            this._clipWidth = param1;
            callLater(this.changeClip);
            return;
        }// end function

        public function get clipHeight() : Number
        {
            return this._clipHeight;
        }// end function

        public function set clipHeight(param1:Number) : void
        {
            this._clipHeight = param1;
            callLater(this.changeClip);
            return;
        }// end function

        protected function changeClip() : void
        {
            if (App.asset.hasClass(this._url))
            {
                this.loadComplete(this._url, App.asset.getBitmapData(this._url, false));
            }
            else
            {
                App.mloader3.loadBMD(this._url, 3, new Handler(this.loadComplete, [this._url]));
            }
            return;
        }// end function

        protected function loadComplete(param1:String, param2:BitmapData) : void
        {
            if (param1 == this._url && param2)
            {
                if (!isNaN(this._clipWidth))
                {
                    this._clipX = Math.ceil(param2.width / this._clipWidth);
                }
                if (!isNaN(this._clipHeight))
                {
                    this._clipY = Math.ceil(param2.height / this._clipHeight);
                }
                App.asset.cacheBitmapData(param1, param2);
                this.clips = App.asset.getClips(param1, this._clipX, this._clipY);
                App.asset.disposeBitmapData(param1);
            }
            return;
        }// end function

        public function get clips() : Vector.<BitmapData>
        {
            return this._bitmap.clips;
        }// end function

        public function set clips(param1:Vector.<BitmapData>) : void
        {
            if (param1)
            {
                this._bitmap.clips = param1;
                _contentWidth = this._bitmap.width;
                _contentHeight = this._bitmap.height;
            }
            sendEvent(UIEvent.IMAGE_LOADED);
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            super.width = param1;
            this._bitmap.width = param1;
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            super.height = param1;
            this._bitmap.height = param1;
            return;
        }// end function

        override public function commitMeasure() : void
        {
            exeCallLater(this.changeClip);
            return;
        }// end function

        public function get sizeGrid() : String
        {
            if (this._bitmap.sizeGrid)
            {
                return this._bitmap.sizeGrid.join(",");
            }
            return null;
        }// end function

        public function set sizeGrid(param1:String) : void
        {
            this._bitmap.sizeGrid = StringUtils.fillArrayByString(Styles.defaultSizeGrid, param1);
            return;
        }// end function

        public function get frame() : int
        {
            return this._bitmap.index;
        }// end function

        public function set frame(param1:int) : void
        {
            var _loc_2:Handler = null;
            this._bitmap.index = param1;
            if (this._bitmap.index == this._to)
            {
                this.stop();
                this._to = -1;
                if (this._complete != null)
                {
                    _loc_2 = this._complete;
                    this._complete = null;
                    _loc_2.execute();
                }
            }
            return;
        }// end function

        public function get totalFrame() : int
        {
            return this._bitmap.clips ? (this._bitmap.clips.length) : (0);
        }// end function

        public function get autoStopAtRemoved() : Boolean
        {
            return this._autoStopAtRemoved;
        }// end function

        public function set autoStopAtRemoved(param1:Boolean) : void
        {
            this._autoStopAtRemoved = param1;
            return;
        }// end function

        public function get autoPlay() : Boolean
        {
            return this._autoPlay;
        }// end function

        public function set autoPlay(param1:Boolean) : void
        {
            if (this._autoPlay != param1)
            {
                this._autoPlay = param1;
                if (this._autoPlay)
                {
                    this.play();
                }
                else
                {
                    this.stop();
                }
            }
            return;
        }// end function

        public function get interval() : int
        {
            return this._interval;
        }// end function

        public function set interval(param1:int) : void
        {
            if (this._interval != param1)
            {
                this._interval = param1;
                if (this._isPlaying)
                {
                    this.play();
                }
            }
            return;
        }// end function

        public function get isPlaying() : Boolean
        {
            return this._isPlaying;
        }// end function

        public function set isPlaying(param1:Boolean) : void
        {
            this._isPlaying = param1;
            return;
        }// end function

        public function play() : void
        {
            this._isPlaying = true;
            this.frame = this._bitmap.index;
            App.timer.doLoop(this._interval, this.loop);
            return;
        }// end function

        protected function loop() : void
        {
            var _loc_1:String = this;
            var _loc_2:* = this.frame + 1;
            _loc_1.frame = _loc_2;
            return;
        }// end function

        public function stop() : void
        {
            App.timer.clearTimer(this.loop);
            this._isPlaying = false;
            return;
        }// end function

        public function gotoAndPlay(param1:int) : void
        {
            this.frame = param1;
            this.play();
            return;
        }// end function

        public function gotoAndStop(param1:int) : void
        {
            this.stop();
            this.frame = param1;
            return;
        }// end function

        public function playFromTo(param1:int = -1, param2:int = -1, param3:Handler = null) : void
        {
            this._from = param1 == -1 ? (0) : (param1);
            this._to = param2 == -1 ? (this._clipX * this._clipY - 1) : (param2);
            this._complete = param3;
            this.gotoAndPlay(this._from);
            return;
        }// end function

        override public function set dataSource(param1:Object) : void
        {
            _dataSource = param1;
            if (param1 is int || param1 is String)
            {
                this.frame = int(param1);
            }
            else
            {
                super.dataSource = param1;
            }
            return;
        }// end function

        public function get smoothing() : Boolean
        {
            return this._bitmap.smoothing;
        }// end function

        public function set smoothing(param1:Boolean) : void
        {
            this._bitmap.smoothing = param1;
            return;
        }// end function

        public function get bitmap() : AutoBitmap
        {
            return this._bitmap;
        }// end function

        public function dispose(param1:Boolean = false) : void
        {
            this._bitmap.bitmapData = null;
            App.asset.destroyClips(this._url);
            if (param1)
            {
                App.loader.clearResLoaded(this._url);
            }
            return;
        }// end function

    }
}
